import { GetInProduction, DelInProduction } from "@/api";

export const useContent = () => {
    const userStore = useUserStore();
    const pageStore = usePageStore();
    const isLoading = ref(false);
    const list = ref([]);
    const keyword = ref("");
    const tabActive = ref(1);
    const tabList = reactive([
        {
            label: "3D展厅",
            value: 1,
        },
        {
            label: "互动课件",
            value: 2,
        },
    ]);

    if (userStore.userInfo.level == 3) {
        tabList.push(
            {
                label: "消防作品",
                value: 4,
            },
            {
                label: "湛江作品",
                value: 5,
            },
            {
                label: "厦门电力作品",
                value: 6,
            },
            {
                label: "冶金作品",
                value: 7,
            },
            {
                label: "数据作品",
                value: 8,
            },
            {
                label: "医疗作品",
                value: 9,
            },
            {
                label: "水务作品",
                value: 10,
            },
            {
                label: "综合作品",
                value: 11,
            },
        );
    }

    const GetList = async () => {
        isLoading.value = true;
        const res = await GetInProduction({
            name: keyword.value,
            type: tabActive.value,
        });
        isLoading.value = false;
        if (res.code == 200) {
            list.value = res.data;
        }
    };

    const onJumpUrl = (url) => {
        let newUrl = GetTemplateLink(url, tabActive.value);

        if (url) {
            if (pageStore.isMobile) {
                Message().warning("请在电脑端网站打开！网址www.ykjhr.com");
            } else {
                window.open(newUrl);
            }
        } else {
            Message().error("url地址错误！");
        }
    };

    const onDelMyWork = async ({ id }) => {
        const res = await DelInProduction({
            id,
        });

        if (res.code == 200) {
            Message().success("删除成功！");
            GetList();
        } else {
            Message().error("删除失败！");
        }
    };

    const onSearch = (value) => {
        keyword.value = value;
        GetList();
    };

    watch(tabActive, () => {
        GetList();
    });

    return {
        isLoading,
        list,
        keyword,
        userStore,
        tabActive,
        tabList,
        onJumpUrl,
        onDelMyWork,
        onSearch,
        GetList,
    };
};
